package com.artron.mediaartron.ui.fragment.made;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.FrameData;
import com.artron.mediaartron.data.production.FrameConstant;
import com.artron.mediaartron.data.production.SuitableSizeHelper;
import com.artron.mediaartron.ui.activity.CustomSelectionActivity;
import com.artron.mediaartron.ui.activity.EditPageActivity;
import com.artron.mediaartron.ui.activity.PictureSelectionActivity;
import com.artron.mediaartron.ui.widget.CustomImageView;
import com.bilibili.boxing.Boxing;
import com.bilibili.boxing.model.config.BoxingConfig;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import butterknife.BindView;
import me.kaelaela.verticalviewpager.VerticalViewPager;

import static com.artron.mediaartron.ui.activity.PictureSelectionActivity.IS_HAVE_TAB_FRAGMENT;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-21.
 */

public class SelectionHorizontalFragment extends BaseStaticFragment implements CustomImageView.OnUpdateDrawableCallback {

    public static final int REQUEST_CODE = 1000;

    @BindView(R.id.SelectionHorizontalFragment_iv_frame)
    protected ImageView mIvFrame;
    @BindView(R.id.SelectionHorizontalFragment_iv_image)
    protected CustomImageView mIvImage;
    @BindView(R.id.SelectionHorizontalFragment_iv_add)
    protected ImageView mIvAdd;
    private FrameData mData;

    private String mPath;

    public static SelectionHorizontalFragment newInstance(FrameData frame) {
        SelectionHorizontalFragment fragment = new SelectionHorizontalFragment();
        fragment.init(frame);
        return fragment;
    }

    private void init(FrameData frame) {
        this.mData = frame;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_custom_selection_item;
    }

    @Override
    protected void initView() {
        mIvFrame.setImageResource(mData.getImage().getFrameResourceId());
        if (mData.getPagerHeight() != 0) {
           initImageWidthAndHeight(mData.getPagerHeight());
        }

        mPath = mData.getImage().getContentImage();
        if (!TextUtils.isEmpty(mPath)) {
            updateImageViewContent(mPath);
        }
        mIvImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mPath)) {
                    BoxingConfig config = new BoxingConfig(BoxingConfig.Mode.SINGLE_IMG)
                            .needCamera(R.drawable.ic_selection_picture_camera)
                            .withMaxCount(10);
                    Boxing boxing = Boxing.of(config).withIntent(mContext, PictureSelectionActivity.class);
                    Intent intent = boxing.getIntent();
                    intent.putExtra(IS_HAVE_TAB_FRAGMENT, true);
                    boxing.start((Activity) mContext, REQUEST_CODE);
                } else {
                    EditPageActivity.startForResult(((Activity) mContext), mData, REQUEST_CODE);
                }
            }
        });

        VerticalViewPager pager = ((CustomSelectionVerticalFragment) getParentFragment()).getVerticalViewPager();
        mIvImage.setParentPager(pager);

        mIvImage.setOnUpdateDrawableCallback(this);
    }

    public void initImageWidthAndHeight(float pagerHeight) {
        float per = pagerHeight / 800;
        ViewGroup.LayoutParams frameParams = mIvFrame.getLayoutParams();
        FrameLayout.LayoutParams imageParams = (FrameLayout.LayoutParams)
                mIvImage.getLayoutParams();

        int type = mData.getType();
        if (type == CustomSelectionActivity.CUSTOM_MADE) {
            int distance = (int) FrameConstant.BLANK_CUSTOM_MADE;
            frameParams.height = (int) (per * mData.getHeight());
            frameParams.width = (int) (per * mData.getWidth());
            imageParams.height = (int) ((mData.getHeight() - distance) * per);
            imageParams.width = (int) ((mData.getWidth() - distance) * per);
        } else if (type == CustomSelectionActivity.POSTER) {
            frameParams.height = (int) (per * mData.getHeight() + 50);
            frameParams.width = (int) (per * mData.getWidth()  + 50);
            imageParams.height = (int) (mData.getHeight() * per);
            imageParams.width = (int) (mData.getWidth() * per);
            imageParams.leftMargin = 5;
            imageParams.topMargin = 5;
        }

        mIvFrame.setLayoutParams(frameParams);
        mIvImage.setLayoutParams(imageParams);
    }

    public void updateImageViewContent(String path) {
        mPath = path;
        SuitableSizeHelper helper = SuitableSizeHelper.getHelper();
        Bitmap clipBitmap = helper.getClipBitmap();
        if (clipBitmap != null) {
            mIvImage.setImageBitmap(clipBitmap);
        } else {
            Glide.with(mContext)
                    .load(path)
                    .into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource,
                                                    GlideAnimation<? super GlideDrawable> glideAnimation) {
                            mIvImage.setImageDrawable(resource);
                        }
                    });
        }
        mIvAdd.setVisibility(View.GONE);
    }

    @Override
    public void updateDrawable(int width, int height) {
        if (TextUtils.isEmpty(mPath)) {
            return;
        }

        float imageHeight, imageWidth;
        if (((float) width) / mData.getWidth() >
                ((float) height) / mData.getHeight()) {
            imageHeight = height;
            imageWidth = height * mData.getWidth() / mData.getHeight();
        } else {
            imageWidth = width;
            imageHeight = width * mData.getHeight() / mData.getWidth();
        }

        boolean suitableSize = SuitableSizeHelper.getHelper().isSuitableSize(mData.getWidth(),
                mData.getHeight(), imageWidth, imageHeight);

        ((CustomSelectionVerticalFragment) getParentFragment()).updateTips(mData, !suitableSize);
    }

    public void updateDrawable() {
        Drawable drawable = mIvImage.getDrawable();
        if (drawable != null) {
            updateDrawable(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        }
    }
}
